# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: save_setting_2_ini.py
  @time: 2022/4/16 14:03
  @desc:
"""
import configparser


def save_ini_info(ini_path, section, option, value, mode="w"):
    con = configparser.ConfigParser()
    con.read(ini_path, encoding='utf-8-sig')
    if value:
        content = value
    else:
        content = ""
    if not con.has_section(section):
        con.add_section(section)
    con.set(section, option, content)
    with open(ini_path, mode, encoding='utf-8-sig') as f:
        con.write(f)


def save_data2_2_ini(ini_path, section, dict_data: dict, mode="w"):
    try:
        con = configparser.ConfigParser()
        con.read(ini_path, encoding='utf-8-sig')
        if not con.has_section(section):
            con.add_section(section)
        for key, value in dict_data.items():
            con.set(section, key, str(value))
            with open(ini_path, mode, encoding='utf-8-sig') as f:
                con.write(f)
    except Exception as e:
        raise e
